package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.Catalog;
import cn.wolfcode.shop.mapper.CatalogMapper;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.service.IProductService;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.shop.util.RedisConstants;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import java.util.List;

/**
 * Created by wolfcode on 2018/8/15.
 */
@Service
@Transactional
public class CatalogServiceImpl implements ICatalogService {
    @Autowired
    private CatalogMapper catalogMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IPropertyService propertyService;
    @Autowired
    private IProductService productService;
    public List<Catalog> selectALl() {
        return catalogMapper.selectAll();
    }

    public String selectAllFromCache() {
        String catalogAllJSONString = (String) redisTemplate.opsForValue().get(RedisConstants.CATALOG_ALL);
        if(catalogAllJSONString==null){
            catalogAllJSONString = refreshCache();
        }
        return catalogAllJSONString;
    }
    private String refreshCache(){
        String catalogAllJSONString = JSON.toJSONString(catalogMapper.selectAll());
        redisTemplate.opsForValue().set(RedisConstants.CATALOG_ALL,catalogAllJSONString);
        return catalogAllJSONString;
    }

    public List<Catalog> queryByPId(Long catalogId) {
        List<Catalog> catalogs = catalogMapper.queryByPId(catalogId);
        String propertyCountKey;
        String productCountKey;
        for(Catalog catalog:catalogs){
            propertyCountKey = MessageFormat.format(RedisConstants.CATALOG_PROPERTY_COUNT,catalog.getId());
            Integer propertyCount = (Integer) redisTemplate.opsForValue().get(propertyCountKey);
            if(propertyCount==null){
                propertyCount = propertyService.queryCountByCatalogId(catalog.getId());
                redisTemplate.opsForValue().set(propertyCountKey,propertyCount);
            }
            catalog.setPropertyCount(propertyCount);

            productCountKey = MessageFormat.format(RedisConstants.CATALOG_PRODUCT_COUNT,catalog.getId());
            Integer productCount = (Integer) redisTemplate.opsForValue().get(productCountKey);
            if(productCount==null){
                productCount = productService.queryCountByCatalogId(catalog.getId());
                redisTemplate.opsForValue().set(productCountKey,productCount);
            }
            catalog.setProductCount(productCount);
        }
        return catalogs;
    }

    public void updateSort(List<Long> ids) {
        Catalog catalog;
        for (int i = 0; i < ids.size(); i++) {
            catalog = catalogMapper.selectByPrimaryKey(ids.get(i));
            catalog.setSort(i + 1);
            catalogMapper.updateByPrimaryKey(catalog);
        }
        refreshCache();
    }

    public void saveOrUpdate(Catalog catalog) {
        if (catalog.getId() == null) {
            //新增
            //判断pId对应的元素是否是叶子节点.
            Catalog parentCatalog = catalogMapper.selectByPrimaryKey(catalog.getPId());
            //如果是叶子节点,需要修改isParent属性===>true
            if(!parentCatalog.isParent()){
                parentCatalog.setParent(true);
                catalogMapper.updateByPrimaryKey(parentCatalog);
            }
            catalogMapper.insert(catalog);
        } else {
            //更新
            catalogMapper.updateByPrimaryKey(catalog);
        }
        refreshCache();
    }

    public void delete(Long catalogId) {
        Catalog parentCatalog = catalogMapper.getParentByChildId(catalogId);
        int count = catalogMapper.queryChildCountById(parentCatalog.getId());
        if(count==1){
            parentCatalog.setParent(false);
            catalogMapper.updateByPrimaryKey(parentCatalog);
        }
        /*int count = catalogMapper.queryChildCountByChildId(catalogId);
        if(count==1){
            catalogMapper.updateParentStateByChildId(catalogId,false);
        }*/
        catalogMapper.deleteByPrimaryKey(catalogId);
        refreshCache();

    }
}
